package com.bruce.controller;

import com.alibaba.fastjson.JSON;
import com.bruce.consts.CommonConst;
import com.bruce.entity.Response;
import com.bruce.service.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AccountController {
    @Autowired
    private SysService sysService;

    @RequestMapping(value="/login",method = RequestMethod.GET)
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("login");
        return mav;
    }

    @RequestMapping(value="/account/login")
    @ResponseBody
    public String accountlogin(@RequestParam("username") String username,@RequestParam("password") String password) {
        Response response = new Response();
        try
        {
            if(sysService.getSysUser(username,password) != null)
            {
                response.setSuccess(true);
                response.setMsg(CommonConst.Success);
                response.setUrl("/");
            }
            else
            {
                response.setSuccess(false);
                response.setMsg("账户名或密码错误，请重新输入");
            }
        }
        catch (Exception ex)
        {
            response.setSuccess(false);
            response.setMsg("系统异常，请稍后重试");
        }
        return JSON.toJSONString(response);
    }
}
